
package com.youlai.system.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.system.common.model.Option;
import com.youlai.system.converter.MoldConverter;
import com.youlai.system.mapper.SysMoldMapper;
import com.youlai.system.model.entity.SysMold;
import com.youlai.system.model.form.MoldForm;
import com.youlai.system.model.query.MoldPageQuery;
import com.youlai.system.model.vo.MoldPageVO;
import com.youlai.system.service.SysMoldService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 故障类型业务接口实现
 */
@Service
@RequiredArgsConstructor
public class SysMoldServiceImpl extends ServiceImpl<SysMoldMapper, SysMold> implements SysMoldService {

    private final MoldConverter moldConverter;

    @Override
    public SysMold getByContentEqual(String content) {
        LambdaQueryWrapper<SysMold> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysMold::getName, content);
        return getOne(queryWrapper);
    }

    @Override
    public Long saveByContent(String content) {
        SysMold mold = new SysMold();
        mold.setName(content);
        save(mold);
        return mold.getId();
    }

    @Override
    public Page<MoldPageVO> getMoldPage(MoldPageQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        String keywords = queryParams.getKeywords();

        LambdaQueryWrapper<SysMold> queryWrapper = new LambdaQueryWrapper<SysMold>()
                .and(StrUtil.isNotBlank(keywords),
                        wrapper ->
                                wrapper.like(StrUtil.isNotBlank(keywords), SysMold::getName, keywords)
                                        .or()
                                        .like(StrUtil.isNotBlank(keywords), SysMold::getCode, keywords)
                );
        //查询数据
        Page<SysMold> moldPage = this.page(new Page<>(pageNum, pageSize), queryWrapper);
        return moldConverter.entity2Page(moldPage);
    }

    @Override
    public boolean saveMold(MoldForm moldForm) {
        String name = moldForm.getName();
        long nameCount = this.count(new LambdaQueryWrapper<SysMold>().eq(SysMold::getName, name));
        Assert.isTrue(nameCount == 0, "故障类型名称已存在");

        String code = moldForm.getCode();
        long codeCount = this.count(new LambdaQueryWrapper<SysMold>().eq(SysMold::getCode, code));
        Assert.isTrue(codeCount == 0, "故障类型编号已存在");

        // 实体转换
        SysMold mold = moldConverter.form2Entity(moldForm);
        return save(mold);
    }

    @Override
    public boolean updateMold(Long moldId, MoldForm moldForm) {
        String name = moldForm.getName();
        long nameCount = this.count(new LambdaQueryWrapper<SysMold>()
                .eq(SysMold::getName, name)
                .ne(SysMold::getId, moldId)
        );
        Assert.isTrue(nameCount == 0, "故障类型名称已存在");

        String code = moldForm.getCode();
        long codeCount = this.count(new LambdaQueryWrapper<SysMold>()
                .eq(SysMold::getCode, code)
                .ne(SysMold::getId, moldId));
        Assert.isTrue(codeCount == 0, "故障类型编号已存在");

        // form -> entity
        SysMold entity = moldConverter.form2Entity(moldForm);

        // 修改故障类型
        return this.updateById(entity);
    }

    @Override
    public MoldForm getMoldForm(Long moldId) {
        SysMold entity = this.getById(moldId);
        return moldConverter.entity2Form(entity);
    }

    @Override
    public boolean deleteMolds(String idsStr) {
        Assert.isTrue(StrUtil.isNotBlank(idsStr), "故障类型删除数据为空");
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(ids);
    }

    @Override
    public boolean deleteMolds(List<Long> idList) {
        return this.removeByIds(idList);
    }

    @Override
    public List<Option> listMoldOptions() {
        // 查询数据
        List<SysMold> moldList = this.list(new LambdaQueryWrapper<SysMold>()
                .select(SysMold::getId, SysMold::getName)
                .orderByAsc(SysMold::getSort)
        );

        // 实体转换
        return moldConverter.entities2Options(moldList);
    }
}
